package com.sjq.yqgl.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import com.sjq.yqgl.dao.YqxfDao;
import com.sjq.common.dao.GenericDao;
import com.sjq.common.util.PageResults;
import com.sjq.yqgl.entity.Yqxf;

@Repository
public class YqxfDaoImpl extends GenericDao<Yqxf> implements YqxfDao {

	 @Override
	 public PageResults<?> selectYqxfByPage(int pageNo, int pageSize,Yqxf yqxf){
	 	StringBuilder paramSql = new StringBuilder();
		List params = new ArrayList();
		//查询条件
		/**/
		if (yqxf.getXxzjbh() != null&& !(yqxf.getXxzjbh().equals(""))) {
			paramSql.append(" and xxzjbh=? ");
			params.add(yqxf.getXxzjbh().trim());
		}
		if (yqxf.getGlrwid() != null&& !(yqxf.getGlrwid().equals(""))) {
			paramSql.append(" and glrwid=? ");
			params.add(yqxf.getGlrwid().trim());
		}
		
		if (yqxf.getSfhm() != null&& !(yqxf.getSfhm().equals(""))) {
			paramSql.append(" and sfhm=? ");
			params.add(yqxf.getSfhm().trim());
		}	
		
		if (yqxf.getXm() != null&& !(yqxf.getXm().equals(""))) {
			paramSql.append(" and xm=? ");
			params.add(yqxf.getXm().trim());
		}
		if (yqxf.getSjhm()!= null&& !(yqxf.getSjhm().equals(""))) {
			paramSql.append(" and sjhm=? ");
			params.add(yqxf.getSjhm().trim());
		}
		if (yqxf.getXfj()!= null&& !(yqxf.getXfj().equals(""))) {
			paramSql.append(" and xfj=? ");
			params.add(yqxf.getXfj().trim());
		}
		
		if (yqxf.getSjly()!= null&& !(yqxf.getSjly().equals(""))) {
			paramSql.append(" and sjly=? ");
			params.add(yqxf.getSjly().trim());
		}
		
		
		if (yqxf.getTbsj_s() != null&& !(yqxf.getTbsj_s().equals(""))) {
			paramSql.append(" and (XFSJ>?  or XFSJ=?)");
			params.add(yqxf.getTbsj_s().trim());
			params.add(yqxf.getTbsj_s().trim());
		}
		if (yqxf.getTbsj_e() != null&& !(yqxf.getTbsj_e().equals(""))) {
			paramSql.append(" and (XFSJ<?  or XFSJ=?) ");
			params.add(yqxf.getTbsj_e().trim());
			params.add(yqxf.getTbsj_e().trim());
		}
		
		
		
		String sql_fenye="select XXZJBH,XH,XM,SJHM,SFHM,XFJ,PCS,DZ,SJLY,SJGZ,XFSJ,HDGJ from T_YQ_XXXF where DJBGBS <>3 " + paramSql.toString() ;
		PageResults<Map<String, String>> results = new PageResults<Map<String, String>>();
		StringBuilder pageSql = new StringBuilder()
				.append("select * from ")
				.append(" (select tmp_1.*, rownum as rowindex from ("+sql_fenye+") tmp_1 ) tmp_2 ")
				.append(" where rowindex between ")
				.append(((pageNo-1)*pageSize + 1))
				.append(" and ")
				.append(((pageNo - 1) * pageSize + pageSize));
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		try {
			list = this.getListBySql(pageSql.toString(),params.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql="select count(1) as num from T_YQ_XXXF where DJBGBS <>3  "+paramSql.toString()+" ";
		SqlRowSet sqlRowSet = this.getJdbcTemplate().queryForRowSet(sql,params.toArray());
		int count = 0;
		while(sqlRowSet.next()){
			count = sqlRowSet.getInt("num");
		}
		results.setTotalCount(count);
        results.setPageNo(pageNo);
        results.setPageSize(pageSize);
		results.setResults(list);
		return results;	
	 }
	 @Override
	 public int addYqxf(Yqxf yqxf){
		// String sql="select count(1) from T_YQ_XXXF where xm=? and sfhm=? and sjly=? and sjgz=?";//判断重复导入数据
	 	String sql="insert into T_YQ_XXXF ( DJDW,DJDWMC,DJRJH,glrwid,xxzjbh,xh,xm,sjhm,sfhm,xfj,pcs,dz,sjly,sjgz,xfsj,hdgj) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)  ";
	 	Object [] params={yqxf.getDjdw(),yqxf.getDjdwmc(),yqxf.getDjrjh(),yqxf.getGlrwid(),yqxf.getXxzjbh(),yqxf.getXh(),yqxf.getXm(),yqxf.getSjhm(),yqxf.getSfhm(),yqxf.getXfj(),yqxf.getPcs(),yqxf.getDz(),yqxf.getSjly(),yqxf.getSjgz(),yqxf.getXfsj(),yqxf.getHdgj()};
	 	return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	 @Override
	 public int deleteYqxf(String yqxfId){
		 String sql="update T_YQ_XXXF set djbgbs=3 where glrwid=?";
		 Object [] params={ yqxfId};
		 return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	  @Override
	 public int updateYqxf(Yqxf yqxf){
	 	String sql="update T_YQ_XXXF set XXZJBH=?,XH=?,XM=?,SJHM=?,SFHM=?,XFJ=?,PCS=?,DZ=?,SJLY=?,SJGZ=?,XFSJ=?,HDGJ=?  where xxzjbh=?";
	 	Object [] params={yqxf.getXxzjbh(),yqxf.getXh(),yqxf.getXm(),yqxf.getSjhm(),yqxf.getSfhm(),yqxf.getXfj(),yqxf.getPcs(),yqxf.getDz(),yqxf.getSjly(),yqxf.getSjgz(),yqxf.getXfsj(),yqxf.getHdgj()};
	 	return this.getJdbcTemplate().update(sql.toString(),params);
	 }
	 
	 @Override
	 public List<Map<String, String>> findYqxfByListAll(Yqxf yqxf){
		StringBuilder paramSql = new StringBuilder();
		List params = new ArrayList();
		//查询条件
		if (yqxf.getXxzjbh() != null&& !(yqxf.getXxzjbh().equals(""))) {
			paramSql.append(" and xxzjbh=? ");
			params.add(yqxf.getXxzjbh().trim());
		}
		String sql="select XXZJBH,XH,XM,SJHM,SFHM,XFJ,PCS,DZ,SJLY,SJGZ,XFSJ,HDGJ from T_YQ_XXXF where DJBGBS <>3  "+ paramSql.toString() ;
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		try {
			list = this.getListBySql(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}